package com.i2863.tools.weixinmp.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.i2863.tools.weixinmp.domain.NewsInfo;

import java.text.SimpleDateFormat;

/**
 * Created by xuweiwei on 2017/5/10.
 */
public class NewsTemplate {
    static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    public static String getAuthorOrSource(NewsInfo info){
        if(info.getAuthor()!=null && info.getAuthor().length()>0) {
            return "             <span class='titleTime'>作者："
                    + info.getAuthor()
                    + "</span>";
        }else{
            return "             <span class='titleTime'>来源："
                    + info.getSource()
                    + "</span>";
        }
    }
    /**
     * 拼接HTML代码
     * @param news
     * @return
     */
    public static String getHTML(NewsInfo news) {
        String head = "<!DOCTYPE html><html><head>"
                + "    <title>  "
                + news.getTitle()
                + "  </title>"
                + "    <meta http-equiv='Content-Type' content='text/html;charset=UTF-8'/>"
                + "    <meta name='viewport' content='width=device-width, maximum-scale=1.0, initial-scale=1.0, user-scalable=0' />"
                + "    <!--防止手机默认页面显示的数字是电话号码，不改变字体颜色-->"
                + "    <meta name = 'format-detection' content = 'telephone=no'>"
                + "    <!--禁止用户缩放页面-->"
                + "    <meta name='viewport' content='width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no'>"
                + "    <style type='text/css'>"
                + "        *{"
                + "            margin: 0;"
                + "            padding: 0;"
                + "            font-family: '微软雅黑';"
                + "        }"
                + "        .bigDivs{"
                + "            width: 100%;"
                + "            height: 100%;"
                + "            margin: auto;"
                + "            position: relative;"
                + "        }"
                + "        .titleDivs{"
                + "            width: 90%;"
                + "            margin: 10px auto;"
                + "        }"
                + "        .titleSpan{"
                + "            font-size: 22px;"
                + "        }"
                + "        .titleTime{"
                + "            display: inline-block;"
                + "            font-size: 12px;"
                + "            color: #555;"
                + "            text-align: left;"
                + "            padding-top: 10px;"
                + "        }"
                + "        .shiPin{"
                + "            width: 90%;"
                + "            margin: 10px auto;"
                + "        }"
                + "        .shiPin img{"
                + "            width: 100%;"
                + "            height: 100%;"
                + "        }"
                + "        .shiPin p{"
                + "            padding-top: 10px;"
                + "            font-size: 14px;"
                + "            line-height: 22px;"
                + "            text-indent: 2em;"
                + "        }"
                + ""
                + "    </style>"
                + "</head>"
                + "<body>"
                + "     <div class='bigDivs'>"
                + "        <div class='titleDivs'>"
                + "             <span class='titleSpan'>"
                + news.getTitle()
                + "</span><br>"
                + "             <span class='titleTime'>时间："
                + sdf.format(news.getNewsTime())
                + "</span>"
                +getAuthorOrSource(news)

                + "        </div>"
                + "        <div class='shiPin'>";

        String foot = "        </div>" + "     </div>" + "</body>" + ""
                + "</html>";

        StringBuffer html = new StringBuffer();
        html.append(head);

        String newsData = news.getJson();

        JSONArray jsonArray= JSON.parseArray(newsData);



        for (Object obj:jsonArray) {
            JSONObject js  = (JSONObject)obj;
            String contentType = js.getString("contentType");
            if ("img".equals(contentType)) {
                String src = js.getString("src");
                html.append("<img style = 'width:100%' src=' " + src
                        + "  ' alt=''/>");
            } else if ("text".equals(contentType)) {
                String value = js.getString("value");
                html.append("<p>" + value + "</p>");
            }
        }
        html.append(foot);
        return html.toString();
    }

}
